package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.service.AreasService;
import com.dongdongshop.service.CitiesService;
import com.dongdongshop.service.ProvincesService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("address")
public class AddressController {

    @DubboReference
    private AddressService addressService;
    @DubboReference
    private ProvincesService provincesService;
    @DubboReference
    private CitiesService citiesService;
    @DubboReference
    private AreasService areasService;

    @RequestMapping("address")
    public String address(Model model){
        List<TbProvinces> tbProvinces = provincesService.getInfo();
        model.addAttribute("tbProvinces",tbProvinces);
        return "home-setting-address";
    }

    @RequestMapping("selectAddress")
    @ResponseBody
    public Result selectAddress(){
        //判断有没有登录
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        String userId = tbUser.getUsername();
        List<TbAddress> tbAddresses = addressService.selectAddress(userId);
        return Result.ok().setData(tbAddresses);
    }

    @RequestMapping("selectByprovinceid")//二级查询
    @ResponseBody
    public Result selectByprovinceid(String provinceid){
        List<TbCities> tbCities = citiesService.selectByprovinceid(provinceid);
        return Result.ok().setData(tbCities);
    }

    @RequestMapping("selectBycityid")//三级查询
    @ResponseBody
    public Result selectBycityid(String cityid){
        List<TbAreas> tbAreas = areasService.selectBycityid(cityid);
        return Result.ok().setData(tbAreas);
    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(TbAddress tbAddress){
        //判断有没有登录
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        String userId = tbUser.getUsername();
        tbAddress.setUserId(userId);
        addressService.add(tbAddress);
        return Result.ok();
    }

    @RequestMapping("deleteInfo")
    @ResponseBody
    public Result deleteInfo(TbAddress tbAddress){
        addressService.deleteInfo(tbAddress);
        return Result.ok();
    }

    @RequestMapping("getAddress")
    @ResponseBody
    public Result getAddress(){
        //判断有没有登录
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        String userId = tbUser.getUsername();
        List<TbAddress> tbAddresses = addressService.getAddress(userId);
        return Result.ok().setData(tbAddresses);
    }
}
